// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.

package nl.justobjects.pushlet.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Gets events from queue and pushes them on a permanent HTTP connection.
 *
 * @version $Id: PushletSubscriber.java,v 1.4 2003/08/15 08:37:40 justb Exp $
 * @author Just van den Broecke - Just Objects &copy;
 **/
public class PushletSubscriber extends HTTPSubscriber {
	public PushletSubscriber() {
	}

	/** Create client notifier based on "format" parameter passed in request. */
	protected ClientAdapter createClientAdapter(HttpServletRequest httpServletRequest,
												HttpServletResponse httpServletResponse) throws IOException {

         // Assumed to be set by parent.
        String outputFormat = getFormat();

		// Determine client adapter to create.
		if (outputFormat.equals(FORMAT_JAVASCRIPT)) {
			// Client expects to receive Events as JavaScript dispatch calls..
			return BrowserClientAdapter.createPushletAdapter(httpServletRequest, httpServletResponse);
		}
		else if (outputFormat.equals(FORMAT_SERIALIZED_JAVA_OBJECT)) {
			// Client expects to receive Events as Serialized Java Objects.
			return new JavaPushletAdapter();
		}
		else if (outputFormat.equals(FORMAT_XML)) {
			// Client expects to receive Events as XML.
			return new XMLPushletAdapter();
		}
		else {
			return null;
		}
	}

	/** Get events from queue and push to clients. */
	public void processEvents() throws IOException {

		Event nextEvent = null;

		// Main loop: each time a event is received from the Publisher it is written to document.
		while (true) {
			// Get next event; blocks (wait()) until event is returned
			try {
				nextEvent = (Event) eventQueue.deQueue();
			} catch (InterruptedException ie) {
				warn("interrupted");
			}

			if (nextEvent == null) {
				warn("nothing in Queue, bailing out: " + mySubject);
				bailout();
				return;
			}

			// Let clientAdapter determine how to send event
			try {
				clientAdapter.push(nextEvent);
			}
			catch (IOException ioe) {
				// Client may have closed connection.
				// Remove ourselves from Publisher and return.
				// e("register for leave: "+mySubject);
				bailout();
				return;
			}
			catch (Throwable t) {
				warn("register for leave: " + t + " subj=" + mySubject);
				bailout();
				return;
			}
		}
	}

	/** Exceptional print util. */
	public void warn(String s) {
		Log.warn("PushletSubscriber-" + this + ":" + s);
	}
}

/*
 * $Log: PushletSubscriber.java,v $
 * Revision 1.4  2003/08/15 08:37:40  justb
 * fix/add Copyright+LGPL file headers and footers
 *
 * Revision 1.3  2003/08/12 09:57:06  justb
 * replaced all print statements to Log.*() calls
 *
 * Revision 1.2  2003/05/18 16:15:08  justb
 * support for XML encoded Events
 *
 * Revision 1.1.1.1  2002/09/24 21:02:32  justb
 * import to sourceforge
 *
 * Revision 1.1.1.1  2002/09/20 22:48:18  justb
 * import to SF
 *
 * Revision 1.1.1.1  2002/09/20 14:19:04  justb
 * first import into SF
 *
 * Revision 1.4  2002/04/15 20:42:41  just
 * reformatting and renaming GuardedQueue to BlockingQueue
 *
 * Revision 1.3  2000/08/21 20:48:29  just
 * added CVS log and id tags plus copyrights
 *
 *
 */

